package org.xxpay.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.xxpay.core.entity.ChannelAgentMchChannelInfo;
import org.xxpay.core.entity.ChannelAgentMchChannelInfoExample;
import org.xxpay.core.entity.ChannelMchChannelInfo;
import org.xxpay.core.entity.ChannelMchChannelInfoExample;
import org.xxpay.core.service.IChannelAgentMchChannelInfoService;
import org.xxpay.core.service.IChannelMchChannelInfoService;
import org.xxpay.service.dao.mapper.ChannelAgentMchChannelInfoMapper;
import org.xxpay.service.dao.mapper.ChannelMchChannelInfoMapper;

import java.util.List;

/**
 * Created by xl on 2019/5/8.
 */

@Service(interfaceName = "org.xxpay.core.service.IChannelMchChannelInfoService", version = "1.0.0", retries = -1)
public class ChannelMchChannelInfoServiceImpl implements IChannelMchChannelInfoService {
   @Autowired
   private ChannelMchChannelInfoMapper channelMchChannelInfoMapper;
    @Override
    public int add(ChannelMchChannelInfo channelMchChannelInfo) {
        int count =  channelMchChannelInfoMapper.insertSelective(channelMchChannelInfo);
        return count;
    }

    @Override
    public List<ChannelMchChannelInfo> select(int offset, int limit,ChannelMchChannelInfo channelMchChannelInfo) {
        ChannelMchChannelInfoExample example = new ChannelMchChannelInfoExample();
        example.setOrderByClause("createTime DESC");
        example.setOffset(offset);
        example.setLimit(limit);
        ChannelMchChannelInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, channelMchChannelInfo);

        return channelMchChannelInfoMapper.selectByExample(example);

    }
    @Override
    public Integer count(ChannelMchChannelInfo channelMchChannelInfo) {
        ChannelMchChannelInfoExample example = new ChannelMchChannelInfoExample();
        ChannelMchChannelInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, channelMchChannelInfo);
        return channelMchChannelInfoMapper.countByExample(example);
    }

//    @Override
//    public  List<ChannelAgentMchChannelInfo> selectByChannelAgentMchId(Long supChannelMchAgentId) {
//        List<ChannelAgentMchChannelInfo> list =  channelMchChannelInfoMapper.selectByChannelAgentMchId(supChannelMchAgentId);
//        return list;
//    }
    @Override
    public int update(ChannelMchChannelInfo channelMchChannelInfo) {
        return channelMchChannelInfoMapper.updateByPrimaryKeySelective(channelMchChannelInfo);
    }

    @Override
    public ChannelMchChannelInfo find(ChannelMchChannelInfo channelMchChannelInfo) {
        ChannelMchChannelInfoExample example = new ChannelMchChannelInfoExample();
        ChannelMchChannelInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, channelMchChannelInfo);
        List<ChannelMchChannelInfo> channelMchInfoList = channelMchChannelInfoMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(channelMchInfoList)) return null;
        return channelMchInfoList.get(0);
    }
    @Override
    public int batchDeleteChannelMch(List<Long> channelIds) {
        if(CollectionUtils.isEmpty(channelIds)) return 0;
        int deleteCount = 0;
        for(Long channelMchId : channelIds) {
            deleteCount = deleteCount + channelMchChannelInfoMapper.deleteByPrimaryKey(channelMchId);
        }
        return deleteCount;
    }

    void setCriteria(ChannelMchChannelInfoExample.Criteria criteria, ChannelMchChannelInfo channelMchChannelInfo) {
        if(channelMchChannelInfo != null) {
            if(channelMchChannelInfo.getChannelMchId() != null) criteria.andChannelMchIdEqualTo(channelMchChannelInfo.getChannelMchId() );
         //   if(channelAgentMchChannelInfo.getChannelAgentMchRate() != null) criteria.andChannelAgentMchIdEqualTo(channelAgentMchChannelInfo.getChannelAgentMchRate());
            if(channelMchChannelInfo.getPaypassage() != null) criteria.andPaypassageEqualTo(channelMchChannelInfo.getPaypassage());
            if(channelMchChannelInfo.getPayproduct() != null) criteria.andPayproductEqualTo(channelMchChannelInfo.getPayproduct());
            if(channelMchChannelInfo.getStatus() != null && channelMchChannelInfo.getStatus().byteValue() != -99) criteria.andStatusEqualTo(channelMchChannelInfo.getStatus());
        }
    }
}
